package handler

import (
	"github.com/gin-gonic/gin"
	"go-vue-blog-sever/gla"
	"go-vue-blog-sever/utils"
)

func ValidTokens() gin.HandlerFunc {
	return func(c *gin.Context) {
		get := c.Request.Header.Get("Authorization")
		if get == "" {
			c.Abort()
			utils.ResponseBad(c, gla.InsufficientPermissions, nil)
			return
		}
		id, name, password, err := utils.DecodeToken(get)
		if err != nil {
			c.Abort()
			utils.ResponseBad(c, gla.InsufficientPermissions, nil)
			return
		}
		c.Set("userid", id)
		c.Set("username", name)
		c.Set("password", password)
		c.Next()
	}
}
